#!/usr/bin/python2.7

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Author   : Fami
Scripts  : logger.py 
Version  : 0.1
Date     : 3 Nov 2014
Desc     : Consist logging function
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
import time
from modules import utils


#000-Function: Testing Purpose
def printOut(inputMsg):
    print inputMsg


#001-Function: WRITE TO LOG FILES

def logWrite(folderPath, fileName, input_01, input_02, input_03):
    currDate = time.strftime("%d/%m/%Y")
    currTime = time.strftime("%H:%M:%S")
    try:
        fileCheckFlag = utils.checkFile(folderPath, fileName)
        if str(fileCheckFlag) == 'True':
            logFilePath = folderPath+fileName
            file = open(logFilePath, 'a')
            file.writelines('\n['+currDate+','+currTime+']'+input_01+' '+input_02+' '+input_03)
            file.close()
            print 'logger-loggerWrite INFO: File saved successfully'
            exit(2)
        else:
            print 'logger-loggerWrite FAIL: Log files not found!'
            exit(2)
    except IOError as (e):
        print 'logger-loggerWrite ERROR: '+str(e)
        exit(2)
 

##002-Function: PRINT LOG

def logPrint(folderPath, fileName):
    try:
        fileCheckFlag = utils.checkFile(folderPath, fileName)
        if str(fileCheckFlag) == 'True':
            logFilePath = folderPath+fileName
            file = open(logFilePath, 'r')
            print file.read()
            file.close()
            print 'logger-loggerPrint INFO: Log File read succesfully'
            exit(2)
        else:
            print 'logger-loggerPrint FAIL: Log file not found!'
            exit(2)
    except IOError as (e):
        print 'logger-loggerPrint ERROR: '+str(e)
        exit(2)


##003-Function: SEARCH LOG CONTENTS

def logSearchContent(folderPath, fileName, title, keyword):
    try:
        fileCheckFlag = utils.checkFile(folderPath, fileName)
        logFilePath = folderPath+fileName
        searchCount = 0
        textLine = 0
        if (str(fileCheckFlag) == 'True'):
            print title+'\n'
            for line in open(logFilePath, "r"):
                textLine = textLine + 1
                if keyword in line:
                    searchCount = searchCount+1
                    print str(searchCount)+' :'+'line '+str(textLine)+' :'+line
            print 'Total Count: '+str(searchCount)+'\n'
        else:
            print 'logger-loggerSearchContent FAILURE: Log contents search fail.'; exit(2)
    except Exception as (e): print 'logger-loggerSearchContent ERROR: '+str(e); exit(2)

           

